<?
//include "chksession.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="javascript">
function checkInput(){
		if(document.form1.fullname.value ==""){
   				alert("กรุณาป้อนชื่อ-นามสกุล");
   				document.form1.fullname.focus();
   				return false;
		}else if(document.form1.pid.value =="" ||  document.form1.pid.value.length != 13){
   				alert("กรุณาป้อนเลขประจำตัวประชาชนให้ครบถ้้วน");
   				document.form1.pid.focus();
   				return false;
	 	}else if(document.form1.email.value ==""){
   				alert("กรุณาป้อนอีเมล์");
   				document.form1.email.focus();
   				return false;

		}else if(document.form1.pwd.value == ""){
   				alert("กรุณาป้อนรหัสผ่าน");
   				document.form1.pwd.focus();
   				return false;

		}else if(document.form1.user_type.value ==""){
   				alert("กรุณาเลือกประเภทผู้ใช้");
   				document.form1.user_type.focus();
   				return false;
		}else{
   				return true;
		}
	}
	
	
	function check_num(e)
	{
   		var keyPressed;
   		if(window.event){
      		keyPressed = window.event.keyCode; // IE
       		if ((keyPressed < 45) || (keyPressed > 57)){
			 alert("ป้อนได้เฉพาะตัวเลขเท่านั้น");
			 window.event.returnValue = false;
			}
   		}else{
      		keyPressed = e.which; // Firefox      
       		if ((keyPressed < 45) || (keyPressed > 57)){
			 alert("ป้อนได้เฉพาะตัวเลขเท่านั้น");
			 keyPressed = e.preventDefault();
			}
    	}
	}


   function add_user(){
    if(checkInput()){//if checkInput is OK
	//alert("1");
    var pmeters="";
	var email =  document.getElementById("email").value;
	var pwd =  document.getElementById("pwd").value;
	var pid =  document.getElementById("pid").value;
	var fullname =  document.getElementById("fullname").value;
	var user_position =  document.getElementById("user_position").value;
	var private_phone =  document.getElementById("private_phone").value;
	//alert("2");
	var under_org =  document.getElementById("under_org").value;
	var org_address =  document.getElementById("org_address").value;
	var usage_objective =  document.getElementById("usage_objective").value;
	var user_type =  document.getElementById("user_type").value;
	var ref_doc =  document.getElementById("ref_doc").value;
	var active =  document.getElementById("active").checked;
	//alert("3");
	//alert(active);
	if(active) active="1"; else active="0";
	//alert(active);
	var regis_dte =  "255211042350";
	var cmm =  document.getElementById("cmm").value;
	var prvid =  document.getElementById("province").value;
	var ampid =  document.getElementById("amphur").value;
	//alert("prvid"+prvid);
	//alert("ampid"+ampid);
//alert("before create object");
	var urlstr="add_user2.php";
	if (window.XMLHttpRequest) {
   		request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
   		request = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	//alert("after create object");

	//var request = GXmlHttp.create();
	pmeters="email="+email+"&pwd="+pwd+"&pid="+pid+"&fullname="+fullname+"&user_position="+user_position+"&private_phone="+private_phone+"&under_org="+under_org+"&org_address="+org_address+"&usage_objective="+usage_objective+"&user_type="+user_type+"&ref_doc="+ref_doc+"&active="+active+"&regis_dte="+regis_dte+"&cmm="+cmm+"&prvid="+prvid+"&ampid="+ampid;
	//alert("before send");
	request.open('POST', urlstr , true); // request XML from PHP with AJAX call          
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");           
	request.setRequestHeader("Content-length", pmeters.length);            
	request.setRequestHeader("Connection", "close"); 
	
	request.send(pmeters); 
	request.onreadystatechange = function () { // if function
	//alert("wait");
	//alert(request.readyState);
	if (request.readyState == 4) {//if readyState == 4
			//var xmlDoc = request.responseXML;
			//message = xmlDoc.documentElement.getElementsByTagName("message");
			var textResponse=request.responseText;
			//message = textResponse.documentElement.getElementsByTagName("message");
			//alert(textResponse);
			var arr_response = textResponse.split(':');
			if(arr_response[0]=="1"){
			   	alert("เพิ่มผู้ใช้เสร็จเรียบร้อย");
			   	document.location = "index.php?tab=manageuser&tab2=0";
			}else{
				alert(arr_response[1]);
			}
			//document.write(textResponse);
			
		}// end if readyState == 4
	}// end function
	request.send(null); 
   }//if checkInput is OK
}// 

 function edit_user(){
    if(checkInput()){//if checkInput is OK
    var pmeters="";
	var email =  document.getElementById("email").value;
	var pwd =  document.getElementById("pwd").value;
	var pid =  document.getElementById("pid").value;
	var fullname =  document.getElementById("fullname").value;
	var user_position =  document.getElementById("user_position").value;
	var private_phone =  document.getElementById("private_phone").value;
	var under_org =  document.getElementById("under_org").value;
	var org_address =  document.getElementById("org_address").value;
	var usage_objective =  document.getElementById("usage_objective").value;
	var user_type =  document.getElementById("user_type").value;
	var ref_doc =  document.getElementById("ref_doc").value;
	var active =  document.getElementById("active").checked;
	//alert(active);
	if(active) active="1"; else active="0";
	//alert(active);
	var regis_dte =  "2552-11-04-23:50";
	var cmm =  document.getElementById("cmm").value;
	var prvid =  document.getElementById("province").value;
	var ampid =  document.getElementById("amphur").value;
	var userid =  document.getElementById("userid").value;
	//alert("prvid"+prvid);
	//alert("ampid"+ampid);

	var urlstr="edit_user2.php";
	if (window.XMLHttpRequest) {
   		request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
   		request = new ActiveXObject("Microsoft.XMLHTTP");
	} 

	//var request = GXmlHttp.create();
	pmeters="email="+email+"&pwd="+pwd+"&pid="+pid+"&fullname="+fullname+"&user_position="+user_position+"&private_phone="+private_phone+"&under_org="+under_org+"&org_address="+org_address+"&usage_objective="+usage_objective+"&user_type="+user_type+"&ref_doc="+ref_doc+"&active="+active+"&regis_dte="+regis_dte+"&cmm="+cmm+"&prvid="+prvid+"&ampid="+ampid+"&userid="+userid;

	request.open('POST', urlstr , true); // request XML from PHP with AJAX call          
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");           
	request.setRequestHeader("Content-length", pmeters.length);            
	request.setRequestHeader("Connection", "close"); 
	
	request.send(pmeters); 
	request.onreadystatechange = function () { // if function
	//alert("wait");
	//alert(request.readyState);
	if (request.readyState == 4) {//if readyState == 4
			//var xmlDoc = request.responseXML;
			//message = xmlDoc.documentElement.getElementsByTagName("message");
			var textResponse=request.responseText;
			//message = textResponse.documentElement.getElementsByTagName("message");
			//alert(textResponse);
			var arr_response = textResponse.split(':');
			if(arr_response[0]=="1"){
			   	alert("บันทึกข้อมูลเสร็จเรียบร้อย");
			   	document.location = "index.php?tab=manageuser&tab2=0";
			}else{
				alert(arr_response[1]);
			}
			//document.write(textResponse);
			
		}// end if readyState == 4
	}// end function
	request.send(null); 
   }//if checkInput is OK
}// 

function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function dochange(src, val) {
     var req = Inint_AJAX();
     req.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    document.getElementById(src).innerHTML=req.responseText; //รับค่ากลับมา
					//alert(req.responseText);
               } 
          }
     };
     req.open("GET", "get_ca.php?data="+src+"&val="+val); //สร้าง connection
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
     req.send(null); //ส่งค่า
}

function sendPassword(){
    var pmeters="";
	var email =  document.getElementById("email").value;
	var pwd =  document.getElementById("pwd").value;
	//var pid =  document.getElementById("pid").value;
	var fullname =  document.getElementById("fullname").value;
	if(email != "" && pwd != "" && fullname != ""){//if checkInput is OK
	var urlstr="sendEmail.php";
	if (window.XMLHttpRequest) {
   		request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
   		request = new ActiveXObject("Microsoft.XMLHTTP");
	} 

	//var request = GXmlHttp.create();
	pmeters="email="+email+"&pwd="+pwd+"&fullname="+fullname;

	request.open('POST', urlstr , true); // request XML from PHP with AJAX call          
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");           
	request.setRequestHeader("Content-length", pmeters.length);            
	request.setRequestHeader("Connection", "close"); 
	
	request.send(pmeters); 
	request.onreadystatechange = function () { // if function
	//alert("wait");
	//alert(request.readyState);
	if (request.readyState == 4) {//if readyState == 4
			var textResponse=request.responseText;
			alert(textResponse);
		}// end if readyState == 4
	}// end function
	request.send(null); 
   }//if checkInput is OK
}// 
</script>
</head>
<body>
<?php
$euserid = $_GET["euserid"];
if($euserid<>""){
	//get data
	include "connect/connect_dsccmis.php";
	$sql = "select * from user where userid=".$euserid;
	mysql_query("set names utf8");
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
	$arr_sel_usertype = array("2"=>"","3"=>"","4"=>"","5"=>"");
	$arr_sel_usertype[$row["user_type"]] = "selected";
	if($row["active"]==1) $sts="checked"; else $sts="";
?>
<table cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr valign="top"> 
    <td> <form name="form1" method="post"  onsubmit="return checkInput()"  action="add_user2.php">
        <table border="1" cellspacing="0">
		<tr>
		  <td colspan="4"><strong>ข้อมูลส่วนตัว
		    <input type="hidden" name="userid" id="userid" value="<?=$euserid?>">
		  </strong></td>
		  </tr>
		<tr>
            <td>ชื่อ-นามสกุล<font color="#000000">&nbsp;</font></td>
            <td>
            <input name="fullname" type="text" id="fullname" value="<?=$row["fullname"]?>" size="40">
            <font color="#FF0000">*</font></td>
            <td>หมายเลขบัตรประชาชน</td>
            <td>
              <input name="pid" class="form" readonly="yes" id="pid" onKeyPress="check_num(event);" value="<?=$row["pid"]?>" size="15" maxlength="13" onKeyDown="if (event.keyCode == 17) alert('ไม่อนุญาตให้ทำการ Paste');" onMouseDown="if (event.button == 2) { alert('ไม่อนุญาตให้ทำการ Paste'); this.value=''; }" onpaste="return true;" ondrop="return false;" type="text" />
            <font color="#FF0000">*</font></td>
          </tr>
          
          <tr>
            <td>ตำแหน่ง</td>
            <td colspan="3"><font color="#000000" size="2">
              <input name="user_position" type="text" id="user_position" value="<?=$row["user_position"]?>" size="40">
            </font></td>
          </tr>
          <tr>
            <td>สังกัด</td>
            <td colspan="3"><font color="#000000" size="2">
              <textarea name="under_org" cols="80" rows="3" id="under_org"><?=$row["under_org"]?></textarea>
            </font></td>
          </tr>
          <tr>
            <td>ที่ตั้งหน่วยงาน</td>
            <td colspan="3"><font color="#000000" size="2">
              <textarea name="org_address" cols="80" rows="3" id="org_address"><?=$row["org_address"]?></textarea>
            </font></td>
          </tr>
          <tr>
            <td>เบอร์โทรส่วนตัว</td>
            <td colspan="3"><font color="#000000" size="2">
              <input name="private_phone" type="text" id="private_phone" value="<?=$row["private_phone"]?>" size="40">
            </font></td>
          </tr>
          <tr>
            <td>วัตถุประสงค์การนำไปใช้</td>
            <td colspan="3"><font color="#000000" size="2">
              <textarea name="usage_objective" cols="80" rows="3" id="usage_objective"><?=$row["usage_objective"]?></textarea>
            </font></td>
          </tr>
          <tr>
            <td>เลขที่หนังสือขอใช้</td>
            <td colspan="3"><font color="#000000" size="2">
              <input name="ref_doc" type="text" id="ref_doc" value="<?=$row["ref_doc"]?>" size="40">
            </font></td>
          </tr>
          <tr>
            <td colspan="4"><strong>ข้อมูลผู้ใช้</strong></td>
          </tr>
          <tr>
            <td>อีเมล์</td>
            <td><input name="email" type="text" id="email" readonly="yes"  value="<?=$row["email"]?>" size="40">
                <font color="#FF0000">*</font></td>
            <td>รหัสผ่าน</td>
            <td><font color="#000000" size="2">
              <input name="pwd" type="password" id="pwd" value="<?=$row["pwd"]?>" size="15">
            </font><font color="#FF0000">*</font></td>
          </tr>
          <tr>
            <td align="left">ประเภทสมาชิก</td>
            <td align="left"><select name="user_type" id="user_type">
              <option value="" selected="selected">-- กรุณาเลือก --</option>
              <option value="2" <?=$arr_sel_usertype["2"]?>>เครือข่ายเยียวยา</option>
              <option value="3" <?=$arr_sel_usertype["3"]?>>แหล่งข้อมูล</option>
              <option value="4" <?=$arr_sel_usertype["4"]?>>ผู้บริหาร</option>
			  <option value="5" <?=$arr_sel_usertype["5"]?>>ผู้ดูแลระบบ</option>
            </select>
              <font color="#FF0000">*</font></td>
            <td align="left">&nbsp;</td>
            <td align="left">&nbsp;</td>
          </tr>
          <tr>
            <td>พื้นที่ปฏิบัติงาน</td>
            <td>จังหวัด<font id=provinceall>
              <select name="prvid">
                <option value='0'>========</option>
              </select>
              <?php
				echo "<script>dochange('provinceall','".$row["prvid"]."');\n";
				echo "dochange('amper','".$row["ampid"]."');</script>";
?>
            </font></td>
            <td colspan="2">อำเภอ<font id="amper">
              <select name="ampid">
                <option value='0'>========</option>
              </select>
            </font></td>
          </tr>
          <tr>
            <td align="left">สถานะในการใช้ระบบ</td>
            <td colspan="3" align="left">ใช้ระบบได้<font color="#000000" size="2">
            <input name="active" type="checkbox" id="active" value="1" <?=$sts?>>
            </font></td>
          </tr>
          <tr>
            <td align="left">หมายเหตุ</td>
            <td colspan="3" align="left"><font color="#000000" size="2">
              <textarea name="cmm" cols="80" rows="3" id="cmm"><?=$row["cmm"]?></textarea>
            </font></td>
          </tr>
          <tr>
            <td colspan="4" align="center"><p><font color="#000000" size="2"> <br>
              <input type="button" name="Submit" value="บันทึกข้อมูล" onClick="edit_user();">
                <input type="reset" name="Submit2" value="ยกเลิก" onClick="window.back();">
              </font>
                <input type="button" name="Submit3" value="แจ้งรหัสผ่าน" onClick="sendPassword();">
            </p>            </td>
          </tr>
        </table>
        </form></td>
  </tr>
</table>
<?php
}else{
?>
<table cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr valign="top"> 
    <td> <form name="form1" method="post"  onsubmit="return checkInput()"  action="add_user2.php">
        <table border="1" cellspacing="0">
		<tr>
		  <td colspan="4"><strong>ข้อมูลส่วนตัว</strong></td>
		  </tr>
		<tr>
            <td>ชื่อ-นามสกุล<font color="#000000">&nbsp;</font></td>
            <td>
            <input name="fullname" type="text" id="fullname" size="40">
            <font color="#FF0000">*</font></td>
            <td>หมายเลขบัตรประชาชน</td>
            <td>
              <input name="pid" class="form" id="pid" onKeyPress="check_num(event);" value="" size="15" maxlength="13" onKeyDown="if (event.keyCode == 17) alert('ไม่อนุญาตให้ทำการ Paste');" onMouseDown="if (event.button == 2) { alert('ไม่อนุญาตให้ทำการ Paste'); this.value=''; }" onpaste="return true;" ondrop="return false;" type="text" />
            <font color="#FF0000">*</font></td>
          </tr>
          
          <tr>
            <td>ตำแหน่ง</td>
            <td colspan="3"><font color="#000000" size="2">
              <input name="user_position" type="text" id="user_position" size="40">
            </font></td>
          </tr>
          <tr>
            <td>สังกัด</td>
            <td colspan="3"><font color="#000000" size="2">
              <textarea name="under_org" cols="80" rows="3" id="under_org"></textarea>
            </font></td>
          </tr>
          <tr>
            <td>ที่ตั้งหน่วยงาน</td>
            <td colspan="3"><font color="#000000" size="2">
              <textarea name="org_address" cols="80" rows="3" id="org_address"></textarea>
            </font></td>
          </tr>
          <tr>
            <td>เบอร์โทรส่วนตัว</td>
            <td colspan="3"><font color="#000000" size="2">
              <input name="private_phone" type="text" id="private_phone" size="40">
            </font></td>
          </tr>
          <tr>
            <td>วัตถุประสงค์การนำไปใช้</td>
            <td colspan="3"><font color="#000000" size="2">
              <textarea name="usage_objective" cols="80" rows="3" id="usage_objective"></textarea>
            </font></td>
          </tr>
          <tr>
            <td>เลขที่หนังสือขอใช้</td>
            <td colspan="3"><font color="#000000" size="2">
              <input name="ref_doc" type="text" id="ref_doc" size="40">
            </font></td>
          </tr>
          <tr>
            <td colspan="4"><strong>ข้อมูลผู้ใช้</strong></td>
          </tr>
          <tr>
            <td>อีเมล์</td>
            <td><input name="email" type="text" id="email"  size="40">
                <font color="#FF0000">*</font></td>
            <td>รหัสผ่าน</td>
            <td><font color="#000000" size="2">
              <input name="pwd" type="text" id="pwd" size="15">
            </font><font color="#FF0000">*</font></td>
          </tr>
          <tr>
            <td align="left">ประเภทสมาชิก</td>
            <td align="left"><select name="user_type" id="user_type">
              <option value="" selected="selected">-- กรุณาเลือก --</option>
              <option value="2">เครือข่ายเยียวยา</option>
              <option value="3">แหล่งข้อมูล</option>
              <option value="4">ผู้บริหาร</option>
			  <option value="5">ผู้ดูแลระบบ</option>
            </select>
              <font color="#FF0000">*</font></td>
            <td align="left">&nbsp;</td>
            <td align="left">&nbsp;</td>
          </tr>
          <tr>
            <td>พื้นที่ปฏิบัติงาน</td>
            <td>จังหวัด<font id=provinceall>
              <select name="province" id="province">
                <option value='00'>========</option>
              </select>
              <?php
				echo "<script>dochange('provinceall','".$_POST['province']."')</script>";
?>
            </font></td>
            <td colspan="2">อำเภอ<font id="amper">
              <select name="amphur" id="amphur">
                <option value='00'>========</option>
              </select>
            </font></td>
          </tr>
          <tr>
            <td align="left">สถานะในการใช้ระบบ</td>
            <td colspan="3" align="left">ใช้ระบบได้<font color="#000000" size="2">
            <input name="active" type="checkbox" id="active" value="1">
            </font></td>
          </tr>
          <tr>
            <td align="left">หมายเหตุ</td>
            <td colspan="3" align="left"><font color="#000000" size="2">
              <textarea name="cmm" cols="80" rows="3" id="cmm"></textarea>
            </font></td>
          </tr>
          <tr>
            <td colspan="4" align="center"><p><font color="#000000" size="2"> <br>
              <input type="button" name="Submit" value="บันทึกข้อมูล" onClick="add_user();">
                <input type="reset" name="Submit2" value="ยกเลิก">
            </font></p></td>
          </tr>
        </table>
        </form></td>
  </tr>
</table>
<?php
}
?>
</body>
</html>
